package node_edge

import (
	"errors"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"strings"
	"tupu-go/controller/node_edge/req_res"
	"tupu-go/model"
	"tupu-go/pkg/res"
	service "tupu-go/service/nodeType"
)

// NodeTypeController
// NodeType控制器
type NodeTypeController struct {
	service *service.NodeTypeService
}

func NewNodeTypeController() *NodeTypeController {
	return &NodeTypeController{
		service: service.NewNodeTypeService(), // 通过工厂函数获取接口实例
	}
}

// CreateNodeType 新建节点类型
func (nc *NodeTypeController) CreateNodeType(c *gin.Context) {
	var req req_res.ReqCreateNodeTypeStruct
	if err := c.ShouldBindJSON(&req); err != nil {
		res.ErrorResponse(c, http.StatusBadRequest, err)
		return
	}

	// 调用业务层创建
	nodeType, err := nc.service.CreateNodeType(c.Request.Context(), req)
	if err != nil {
		res.ErrorResponse(c, http.StatusInternalServerError, err)
		return
	}

	res.SuccessResponse(c, nodeType)
}

// ListNodeTypes 获取节点类型列表
func (nc *NodeTypeController) ListNodeTypes(c *gin.Context) {
	// 获取分页参数
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))

	// 调用业务层查询
	nodeTypes, total, err := nc.service.ListNodeTypes(c.Request.Context(), page, pageSize)
	if err != nil {
		res.ErrorResponse(c, http.StatusInternalServerError, err)
		return
	}

	res.SuccessResponse(c, map[string]interface{}{
		"total": total,
		"list":  nodeTypes,
	})
}

// GetNodeType 获取单个节点类型
func (nc *NodeTypeController) GetNodeType(c *gin.Context) {
	// 获取路径参数
	idStr := c.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		res.ErrorResponse(c, http.StatusBadRequest, errors.New("无效的ID"))
		return
	}

	// 调用业务层查询
	nodeType, err := nc.service.GetNodeType(c.Request.Context(), id)
	if err != nil {
		if strings.Contains(err.Error(), "不存在") {
			res.ErrorResponse(c, http.StatusNotFound, err)
		} else {
			res.ErrorResponse(c, http.StatusInternalServerError, err)
		}
		return
	}

	res.SuccessResponse(c, nodeType)
}

// UpdateNodeType 更新节点类型
func (nc *NodeTypeController) UpdateNodeType(c *gin.Context) {
	// 获取路径参数
	idStr := c.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		res.ErrorResponse(c, http.StatusBadRequest, errors.New("无效的ID"))
		return
	}

	// 解析请求体
	var req model.NodeType
	if err := c.ShouldBindJSON(&req); err != nil {
		res.ErrorResponse(c, http.StatusBadRequest, err)
		return
	}

	// 调用业务层更新
	node, err := nc.service.UpdateNodeType(c.Request.Context(), id, req)
	if err != nil {
		res.ErrorResponse(c, http.StatusInternalServerError, err)
		return
	}

	res.SuccessResponse(c, node)
}

// DeleteNodeType 删除节点类型
func (nc *NodeTypeController) DeleteNodeType(c *gin.Context) {
	// 获取路径参数
	idStr := c.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		res.ErrorResponse(c, http.StatusBadRequest, errors.New("无效的ID"))
		return
	}

	// 调用业务层删除
	node, err := nc.service.DeleteNodeType(c.Request.Context(), id)
	if err != nil {
		res.ErrorResponse(c, http.StatusInternalServerError, err)
		return
	}
	res.SuccessResponse(c, node)

}
